package com.atguigu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.House;
import com.atguigu.entity.HouseImage;
import com.atguigu.result.Result;
import com.atguigu.service.HouseImageService;
import com.atguigu.service.HouseService;
import com.atguigu.util.QiniuUtil;
import com.atguigu.vo.HouseVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Controller
@RequestMapping("houseImage")
public class HouseImageController extends BaseController {

    @Reference
    private HouseImageService houseImageService;
    @Reference
    private HouseService houseService;

    @RequestMapping("uploadShow/{id}/{type}")
    public String updateShow(@PathVariable Long id, @PathVariable Long type, Model model) {
        //House house = houseService.getById(id);
        model.addAttribute("houseId", id);
        model.addAttribute("type", type);
        return "house/upload";
    }

    @PostMapping("/upload/{houseId}/{type}")
    @ResponseBody
    public Result upload(@PathVariable Long houseId, @PathVariable Integer type, @RequestParam(value = "file") MultipartFile[] files) throws Exception {


        if (files.length > 0) {
            for (MultipartFile file : files) {

                String substring = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                String newFileName = UUID.randomUUID().toString();
                newFileName += substring;
                // 上传图片
                QiniuUtil.upload2Qiniu(file.getBytes(), newFileName);
                String url = "http://rigj16zfd.hn-bkt.clouddn.com/" + newFileName;

//                HouseImage houseImage = houseImageService.getById(id);
                HouseImage houseImage = new HouseImage();
                houseImage.setHouseId(houseId);
                houseImage.setType(type);
//                houseImage.setImageName(file.getOriginalFilename());
                houseImage.setImageName(newFileName);
                houseImage.setImageUrl(url);
                House house = new House();
                String defaultImageUrl = house.getDefaultImageUrl();
                if (defaultImageUrl == null) {
                    house.setDefaultImageUrl(url);
                }
                house.setId(houseId);
                houseService.update(house);


                houseImageService.insert(houseImage);
            }

        }
        return Result.ok();
    }


    @RequestMapping("delete/{houseId}/{id}")
    public String delete(@PathVariable Long houseId, @PathVariable Long id) {

        HouseImage houseImage = houseImageService.getById(id);

        houseImageService.delete(id);

     /*   String headUrl = houseImage.getImageUrl();
        String[] split = headUrl.split("/");
        for (int i = 0; i < split.length; i++) {
            System.out.println(i+"="+split[i]);
            QiniuUtil.deleteFileFromQiniu(split[3]);
        }*/
        String imageName = houseImage.getImageName();

        QiniuUtil.deleteFileFromQiniu(imageName);

        return "redirect:/house/" + houseId;


    }


}
